/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import dao.CorDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.CorMODEL;
import table.cor.CorTable;

/**
 *
 * @author Welisson
 */
public class CorController extends GenericController{
    
    public boolean salvar(String cor, CorTable ctbl){
        CorMODEL corr = new CorMODEL();
        
        corr.setCor(cor);
        
        try {
            int id = new CorDAO().insert(corr);
            if(id == -1){
                return false;
            }else{
                corr.setId(id);
                ctbl.addCor(corr);
                return true;
            }
            
        } catch (SQLException ex) {
            ex.printStackTrace(System.out);
            return false;
        }
        
    }
    
    public boolean editar(int id, String cor, CorTable ctbl, int linhaSelecionada){
        CorMODEL corr = new CorMODEL();
        corr.setId(id);
        corr.setCor(cor);
        
        if(new CorDAO().update(corr)){
            ctbl.updateCor(linhaSelecionada, corr);
            return true;
        }
        return false;
        
    }
    
    public boolean excluir(int id, CorTable ctbl, int linhaSelecionada){
        
        if(new CorDAO().delete(id)){
            ctbl.removeCor(linhaSelecionada);
            return true;
        }
        return false;
    }
    
    public List<CorMODEL> listar(){
        try {
            return new CorDAO().findCores();
        } catch (SQLException ex) {
            ex.printStackTrace(System.out);
            List<CorMODEL> cor = new ArrayList<CorMODEL>(); 
            return cor;
        }
        
    }
}
